package pe.com.bbva.modpre.persistencia.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import pe.com.bbva.modpre.dominio.quartz.QrtzJobDetails;
import pe.com.bbva.modpre.persistencia.JobDetailsDAO;
import pe.com.bbva.modpre.utileria.Busqueda;

@Repository
@SuppressWarnings("serial")
public class JobDetailsHibernate extends BaseHibernate<QrtzJobDetails, Long> 
implements JobDetailsDAO {
	
	@Autowired
	public JobDetailsHibernate(SessionFactory sessionFactory){
		setSessionFactory(sessionFactory);
	}

	@Override
	public List<QrtzJobDetails> buscarJobDetails(QrtzJobDetails qrtzJobDetails) {
		Busqueda filtro = Busqueda.forClass(QrtzJobDetails.class);
		if (qrtzJobDetails != null) {
			if(qrtzJobDetails.getJobGroup()!=null && StringUtils.isNotBlank(qrtzJobDetails.getJobGroup())){
				filtro.add(Restrictions.ilike("jobGroup",
							"%" + qrtzJobDetails.getJobGroup() + "%"));
			}
			if(qrtzJobDetails.getJobName()!=null && StringUtils.isNotBlank(qrtzJobDetails.getJobName())){
				filtro.add(Restrictions.ilike("jobName",
							"%" + qrtzJobDetails.getJobName() + "%"));
			}
			if (qrtzJobDetails.getDescription() != null && StringUtils.isNotBlank(qrtzJobDetails.getDescription())) {
				filtro.add(Restrictions.ilike("description", "%" + qrtzJobDetails.getDescription() + "%"));
			}
			if (qrtzJobDetails.getJobClassName() != null && StringUtils.isNotBlank(qrtzJobDetails.getJobClassName())) {
				filtro.add(Restrictions.ilike("jobClassName", "%" + qrtzJobDetails.getJobClassName() + "%"));
			}
		}
		return buscar(filtro);
	}
	
}
